<?php
/*
 * Created on Jan 13, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
class ImageUtils
{
	private $width;
	private $height;
	
	function ImageUtils()
	{
	}
	
	public function RatioAdjuct($filename = '', $w = 100, $h = 100, $override = false, $background = false, $defaultImageFile = '', $color = '0xFFFFFF') 
	{
		$output = file_get_contents($filename);
		
		$etag = md5($output);
				
		if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {  // if client has cached this file
			header('HTTP/1.1 304 Not Modified');        // we just send a 304 header, without any data

			exit;
		}
		
		$imageinfo = getImageSize ( $filename );
		
		$imgWidth 	= $imageinfo[0];
		$imgHeight	= $imageinfo[1];
		$imaType	= $imageinfo[2];
		
		if($imgWidth == 0 || $imgHeight == 0){
			if(!empty($defaultImageFile)){
				$imageinfo  = getImageSize ($defaultImageFile);
				
				$imgWidth 	= $imageinfo[0];
				$imgHeight	= $imageinfo[1];
				$imaType	= $imageinfo[2];
				
				$defaultImageFile = $filename;
			}
		}
		
		Header('Content-Type: ' . $imageinfo['mime']);
		header('Etag: ' . $etag);
		header('Cache-Control:max-age=604800');
		
		if(($w <= 0 && $h <=0) || ($w >= $imgWidth && $h >= $imgHeight && !$background)){// out original image			
			$fn = fopen($filename, "r"); 
			fpassthru($fn);
			exit();
			
			$w = $imgWidth;
			$h = $imgHeight;
		}else if($w <= 0 && $h > 0){
			$w = $imgWidth * $h / $imgHeight;
		}else if($h <= 0 && $w > 0){
			$h = $imgHeight * $w / $imgWidth;
		}
		
		$ratioX = $imgWidth / $w;
		$ratioY = $imgHeight / $h;
		
//		echo "ratioX = $ratioX <br>";
//		echo "ratioY = $ratioY <br>";
//		echo "imgWidth = $imgWidth <br>";
//		echo "imgHeight = $imgHeight <br>";
//		exit;
		if ($ratioX > $ratioY || $ratioX == $ratioY) {
			$dst_w = $w;
			$dst_h = ceil ( $imgHeight / $ratioX );
		} else if ($ratioY > $ratioX) {
			$dst_h = $h;
			$dst_w = ceil ( $imgWidth / $ratioY );
		}
		
		//
		switch ($imaType) {
			case 2 :
				$im = imageCreateFromJpeg ( $filename );
				break;
			case 1 :
				$im = imageCreateFromGif ( $filename );
				break;
			
			case 3 :
				$im = imageCreateFromPng ( $filename );
		}
		
		//
		if ($background) {
			//
			$dec = hexdec ( $color );
			$red = 0xFF & ($dec >> 0x10);
			$green = 0xFF & ($dec >> 0x8);
			$blue = 0xFF & $dec;
			
			//
			$dst_pos = array ('d_x' => 0, 'd_y' => 0 );
			($dst_w == $w) ? ($dst_pos ['d_y'] = (($h - $dst_h) / 2)) : ($dst_pos ['d_x'] = (($w - $dst_w) / 2));
			
			$imBox = imageCreateTrueColor ( $w, $h );
			$color_bg = imageColorAllocate ( $imBox, $red, $green, $blue );
			imageFill ( $imBox, 0, 0, $color_bg );
			imageCopyreSampled ( $imBox, $im, $dst_pos ['d_x'], $dst_pos ['d_y'], 0, 0, $dst_w, $dst_h, $imgWidth, $imgHeight );
		} else {
			$imBox = imageCreateTrueColor ( $dst_w, $dst_h );
			imageCopyreSampled ( $imBox, $im, 0, 0, 0, 0, $dst_w, $dst_h, $imgWidth, $imgHeight );
		}
		
		if ($override)
			$filename = str_replace ( strrchr ( $filename, '.' ), '', $filename ) . '_thumb.png';
		
						
		switch ($imaType) {
			case 2 :
				imagejpeg ( $imBox, '', 100);
				break;
			case 1 :
				imagegif ( $imBox);
				break;
			
			case 3 :
				imagepng ( $imBox);
				break;
				
			default: imagejpeg ( $imBox, '', 100);
		}
		 
		imagedestroy($imBox);
	}
	
	/*
	 * 
	 * return array. index 0: new width, index 1: new height
	 */
	public function calculateNewSize($oldWidth, $oldHeight, $maxWidth, $maxHeight, $scale = 15)
	{
		if($oldWidth < 10 || $oldHeight < 10){
			
			if($oldWidth > $maxWidth)
				$this->width = $maxWidth;
			else
				$this->width = $oldWidth;
				
			if($oldHeight > $maxHeight)
				$this->height 	= $maxHeight;
			else
				$this->height	= $oldHeight;
			
			return;
		}
		
		if($oldWidth < $maxWidth and $oldHeight < $maxHeight){
			if($maxWidth / $oldWidth  > $maxHeight / $oldHeight){
				$newHeight = $oldHeight + $oldHeight * ($scale / 100);
				
				if($newHeight > $maxHeight)
					$newHeight = $maxHeight;
				
				$newWidth = ($oldWidth * $newHeight) / $oldHeight;
			}else{
				$newWidth  = $oldWidth + $oldWidth * ($scale / 100);
				
				if($newWidth > $maxWidth)
					$newWidth  = $maxWidth;
				
				$newHeight = ($oldHeight * $newWidth) / $oldWidth;
			}
		}else{
			$oldRate = $oldWidth / $oldHeight;
			$newRate = $maxWidth / $maxHeight;
			
			if($oldRate > $newRate){
				$newWidth  = $maxWidth;
				$newHeight = ($oldHeight * $newWidth) / $oldWidth;
			}else{
				$newHeight = $maxHeight;
				$newWidth = ($oldWidth * $newHeight) / $oldHeight;
			}
		}
		
		$this->width	= $newWidth;
		$this->height	= $newHeight;
	}
	
	public function getWidth()
	{
		return $this->width;
	}
	
	public function getHeight()
	{
		return $this->height;
	}
}
?>
